<template>
  <view class="container">
    <view class="title">注册</view>
    <input class="input" placeholder="请输入用户名" v-model="username" />
    <input class="input" placeholder="请输入6位数字密码" type="password" v-model="password" />
    <input class="input" placeholder="请再次输入密码" type="password" v-model="repeatPassword" />
    <!-- 电话、邮箱、地址输入框 -->
    <input class="input" placeholder="请输入手机号码" v-model="phone" />
    <input class="input" placeholder="请输入邮箱" v-model="email" />
    <input class="input" placeholder="请输入地址" v-model="address" />
    <button class="btn" @click="register">注册</button>
    <navigator url="/pages/login/login" class="link">已有账号？去登录</navigator>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      repeatPassword: '',
      phone: '',
      email: '',
      address: ''
    };
  },
  methods: {
    register() {
      // 1. 校验密码是否为6位数字
      const passwordReg = /^\d{6}$/; // 正则：匹配6位纯数字
      if (!passwordReg.test(this.password)) {
        uni.showToast({
          title: '密码必须是6位数字',
          icon: 'none'
        });
        return;
      }
      
      // 2. 校验两次密码是否一致
      if (this.password !== this.repeatPassword) {
        uni.showToast({
          title: '两次密码不一致',
          icon: 'none'
        });
        return;
      }
      
      // 3. 校验手机号和邮箱（可选）
      if (!this.phone) {
        uni.showToast({
          title: '请输入手机号码',
          icon: 'none'
        });
        return;
      }
      if (!this.email) {
        uni.showToast({
          title: '请输入邮箱',
          icon: 'none'
        });
        return;
      }
      
      // 4. 显示加载提示
      uni.showToast({
        title: '注册中...',
        icon: 'loading',
        mask: true
      });
      
      // 5. 调用云函数提交数据
      uniCloud.callFunction({
        name: 'addUser',
        data: {
          'name': this.username,
          'password': this.password,
          'phone': this.phone,
          'email': this.email,
          'address': this.address
        },
        success: (res) => {
          console.log('注册成功返回：', res.result);
          uni.showToast({
            title: '注册成功',
            icon: 'success'
          });
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/login/login'
            });
          }, 1000);
        },
        fail: (err) => {
          console.error('注册失败：', err);
          uni.showToast({
            title: '注册失败，请重试',
            icon: 'none'
          });
        }
      });
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 100rpx;
}
.title {
  font-size: 36rpx;
  margin-bottom: 40rpx;
}
.input {
  width: 600rpx;
  height: 80rpx;
  border: 1rpx solid #ccc;
  border-radius: 8rpx;
  margin-bottom: 20rpx;
  padding-left: 20rpx;
}
.btn {
  width: 600rpx;
  height: 80rpx;
  background-color: #007AFF;
  color: #fff;
  border-radius: 8rpx;
  margin-bottom: 20rpx;
}
.link {
  font-size: 28rpx;
  color: #007AFF;
}
</style>